package com.wc.AlgoOJ_train.train6_图论.最短路.删点游戏;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/4/15 10:32
 * @description http://43.138.190.70:8888/p/GR0207
 */
public class Main {
    /**
     * 思路： 观察一下数据, 每个点与其他任何一条边都是有路的
     *       倒叙的思想, 他不是一个边一个边的删除, 那我们倒叙, 一个边, 一个边的加入就好了, (NB)
     *       加入的边是可以使得其他边经过这条边
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 310;
    static int[][] d = new int[N][N];
    // 表示一个边是否被加入
    static boolean[] st = new boolean[N];
    static int[] res = new int[N];
    static int[] b = new int[N];
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                d[i][j] = sc.nextInt();
            }
        }
        for (int i = 1; i <= n; i++) b[i] = sc.nextInt();
        for (int l = n; l >= 1; l--) {
            // 相当于可以k为中间的载体
            int k = b[l];
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    d[i][j] = Math.min(d[i][j], d[i][k] + d[k][j]);
                }
            }

            st[k] = true;
            int sum = 0;
            for(int i = 1; i <= n; i++){
                for(int j = 1; j <= n; j++){
                    if (st[i] && st[j]) sum += d[i][j];
                }
            }
            res[l] = sum;
        }
        for(int i = 1; i <= n; i++) out.print(res[i] + " ");
        out.println();
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}